package com.meituan.catering.management.product.biz.validator;

import com.meituan.catering.management.common.exception.BizException;
import com.meituan.catering.management.common.model.enumeration.ErrorCode;
import com.meituan.catering.management.product.api.http.model.request.*;
import com.meituan.catering.management.product.dao.mapper.ProductMapper;
import com.meituan.catering.management.product.dao.model.ProductDO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;


/**
 * @author mac
 */
@Component
public class ProductBizServiceValidator {

    @Resource
    private ProductMapper productMapper;

    public void searchProductValid(Long tenantId, Long userId, SearchProductHttpRequest searchProductHttpRequest) throws BizException {
        paramValid(tenantId, userId, searchProductHttpRequest);
    }

    public void findByIdValid(Long tenantId, Long userId, Long id) throws BizException {
        paramValid(tenantId, userId, id);
    }

    public void create(Long tenantId, Long userId, CreateProductHttpRequest createProductHttpRequest) throws BizException {
        paramValid(tenantId, userId, createProductHttpRequest);
    }

    public void update(Long tenantId, Long userId, Long id, UpdateProductHttpRequest updateProductHttpRequest) throws BizException {
        paramValid(tenantId, userId, id, updateProductHttpRequest);
    }

    public void enable(Long tenantId, Long userId, Long id, EnableProductHttpRequest enableProductHttpRequest) throws BizException {
        enabledValid(tenantId, userId, id, enableProductHttpRequest);
    }

    public void disabled(Long tenantId, Long userId, Long id, DisableProductHttpRequest disableProductHttpRequest) throws BizException {
        disableValid(tenantId, userId, id, disableProductHttpRequest);
    }

    private void paramValid(Long tenantId, Long userId, Object request) throws BizException {
        if (tenantId < 0 || userId < 0 || request == null) {
            throw new BizException(ErrorCode.PARAM_ERROR);
        }
    }

    private void paramValid(Long tenantId, Long userId, Long id) throws BizException {
        if (tenantId < 0 || userId < 0 || id < 0) {
            throw new BizException(ErrorCode.PARAM_ERROR);
        }
        ProductDO productDO = productMapper.findById(tenantId, userId, id);
        if (productDO == null) {
            throw new BizException(ErrorCode.PARAM_ERROR);
        }
    }

    private void paramValid(Long tenantId, Long userId, Long id, UpdateProductHttpRequest request) throws BizException {
        if (tenantId < 0 || userId < 0 || id < 0 || request == null) {
            throw new BizException(ErrorCode.PARAM_ERROR);
        }
        ProductDO productDO = productMapper.findById(tenantId, userId, id);
        if (Objects.isNull(productDO)) {
            throw new BizException(ErrorCode.PARAM_ERROR);
        }
        if (!productDO.getVersion().equals(request.getVersion())) {
            throw new BizException(ErrorCode.VERSION_ERROR);
        }
    }

    private void enabledValid(Long tenantId, Long userId, Long id, EnableProductHttpRequest enableProductHttpRequest) throws BizException {
        if (tenantId < 0 || userId < 0 || id < 0 || enableProductHttpRequest == null) {
            throw new BizException(ErrorCode.PARAM_ERROR);
        }
        ProductDO productDO = productMapper.findById(tenantId, userId, id);
        if (productDO.getEnabled()) {
            throw new BizException(ErrorCode.ENABLED_ERROR);
        }
        if (!productDO.getVersion().equals(enableProductHttpRequest.getVersion())) {
            throw new BizException(ErrorCode.VERSION_ERROR);
        }
    }

    private void disableValid(Long tenantId, Long userId, Long id, DisableProductHttpRequest disableProductHttpRequest) throws BizException {
        if (tenantId < 0 || userId < 0 || id < 0 || disableProductHttpRequest == null) {
            throw new BizException(ErrorCode.PARAM_ERROR);
        }
        ProductDO productDO = productMapper.findById(tenantId, userId, id);
        if (!productDO.getEnabled()) {
            throw new BizException(ErrorCode.DISABLE_ERROR);
        }
        if (!productDO.getVersion().equals(disableProductHttpRequest.getVersion())) {
            throw new BizException(ErrorCode.VERSION_ERROR);
        }
    }
}
